package games.ninJumpDx.ui.actions
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.utils.getTimer;
	
	import games.ninJumpDx.data.GameData;
	import games.ninJumpDx.tools.Tools;
	import utils.ActionFactory;
	import games.ninJumpDx.ui.factorys.ParticleFactory;
	
	import utils.Cartoon;
	import utils.MathTools;
	import utils.particles.Particle;
	
	public class ExplodeParticles extends Cartoon
	{
		/*显示粒子数*/
		private var m_total:Number;
		/*持续时间*/
		private var m_last:Number;
		/*显示粒子的位图数据*/
		private var m_bmd:BitmapData;
		/*粒子列表*/
		private var m_particles:Vector.<Particle>;
		
		public function ExplodeParticles(name:String,n:Number = 40,last:Number = 500)
		{
			super();
			m_particles= new Vector.<Particle>();
			m_total = n;
			m_last = last;
			m_bmd = Tools.getBitmapDataByName(name,GFrame.MAIN_UI);
			init();
		}
		
		override public function update(t:Object=null):void
		{
			if(!start || pause)return;
			var time:Number = getTimer() - m_startTime;
			if(time >= m_last)
			{
				destroy();
			}
		}
		
		override public function destroy(e:* = null):void
		{
			super.destroy();
			while(numChildren > 0)
			{
				removeChildAt(0);
			}
			m_bmd.dispose();
			m_particles = null;
		}
		
		private function init():void
		{
			var particleMaker:ActionFactory = new ParticleFactory();
			for(var i:int=0; i < m_total; i++)
			{
				m_particles.push(Particle(particleMaker.makeAction()));
				m_particles[i].addChild(new Bitmap(m_bmd));
				m_particles[i].getChildAt(0).x = -m_particles[i].width /2;
				m_particles[i].getChildAt(0).y = -m_particles[i].height /2
				makeExplodeParticle(m_particles[i]);
				addChild(m_particles[i]);
			}
		}
		
		private function makeExplodeParticle(particle:Particle):void
		{
			
			var angle:Number = Math.PI * 2 * Math.random();
			var speed:Array = MathTools.getSpeedByVector(GameData.instance.speed * (1+ Math.random()) ,angle);
			particle.setVel(speed[0],speed[1]);
			
			//particle.gravity = 1.5;
			particle.fade = .97;
			particle.drag = 0.98;
			particle.spin = MathTools. randRange(-20,20);
			//particle.directionRotate = true;
			particle.setScaleX(Math.random() * 1.2);
			particle.setScaleY(Math.random() * 1.2);
			
		}
	}
}